package com.hq.fxh.Utils;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.hq.fxh.R;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoActivity;
import com.jph.takephoto.app.TakePhotoImpl;
import com.jph.takephoto.compress.CompressConfig;
import com.jph.takephoto.model.TResult;
import com.jph.takephoto.permission.TakePhotoInvocationHandler;

public class PicSelectActivity extends TakePhotoActivity {
    private TakePhoto takePhoto;
    int maxPic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pic_select);

        maxPic = getIntent().getIntExtra(AppConstant.maxPic, -1);
        if (maxPic <= 0) {
            finish();
        } else  {
            addPic();
        }


    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
        finish();
    }


    @Override
    public void takeSuccess(TResult result) {

        super.takeSuccess(result);
        String compressPath = result.getImage().getCompressPath();
        Intent data = new Intent();
        data.putExtra("data", compressPath);
        this.setResult(AppConstant.requstCode, data);
        finish();
    }

    @Override
    public void takeCancel() {
        super.takeCancel();
        finish();
    }

    private void getCropOptions() {
        CompressConfig compressConfig = new CompressConfig.Builder().setMaxSize(100 * 1024).create();
        takePhoto.onEnableCompress(compressConfig, true);


    }

    public TakePhoto getTakePhoto() {
        if (takePhoto == null) {
            takePhoto = (TakePhoto) TakePhotoInvocationHandler.of(this).bind(new TakePhotoImpl(this, this));
        }
        return takePhoto;
    }

    /* 增加图片*/
    public void addPic() {
        getCropOptions();
        getTakePhoto().onPickMultiple(maxPic);
    }

}
